package com.atguigu.gmall.index.service;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.annotation.GmallCache;
import com.atguigu.gmall.index.feign.GmallPmsClient;
import com.atguigu.gmall.index.uitls.DistributeLock;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class IndexService {

    @Autowired
    private GmallPmsClient gmallPmsClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private DistributeLock distributeLock;

    @Autowired
    private RedissonClient redissonClient;

    private static final String KEY_PREFIX = "index:cates:";

    private static final String LOCK_PREFIX = "index:cates:lock:";

    public List<CategoryEntity> queryCategories() {

        ResponseVo<List<CategoryEntity>> listResponseVo = this.gmallPmsClient.queryCategoriesByPid(0l);
        return listResponseVo.getData();
    }

    @GmallCache(prefix = KEY_PREFIX, timeout = 129600, random = 14400, lock = LOCK_PREFIX)
    public List<CategoryEntity> queryCategorysWithSub(Long pid) {

        ResponseVo<List<CategoryEntity>> listResponseVo = this.gmallPmsClient.queryCategorysWithSub(pid);
        List<CategoryEntity> categoryEntities = listResponseVo.getData();
        return categoryEntities;
    }

    public List<CategoryEntity> queryCategorysWithSub2(Long pid) {
        // 1.先查询缓存，缓存命中直接返回
        String json = this.redisTemplate.opsForValue().get(KEY_PREFIX + pid);
        if (StringUtils.isNotBlank(json)) {
            return JSON.parseArray(json, CategoryEntity.class);
        }

        // 为了防止缓存击穿，添加分布式锁
        RLock fairLock = this.redissonClient.getFairLock(LOCK_PREFIX + pid);
        fairLock.lock();

        try {
            // 获取分布式锁的过程中，可能有其他请求已经把数据放入缓存，此时再次查询缓存
            String json2 = this.redisTemplate.opsForValue().get(KEY_PREFIX + pid);
            if (StringUtils.isNotBlank(json2)) {
                return JSON.parseArray(json2, CategoryEntity.class);
            }

            // 2.如果缓存没有命中，则发送远程请求，查询数据库，并放入缓存
            ResponseVo<List<CategoryEntity>> listResponseVo = this.gmallPmsClient.queryCategorysWithSub(pid);
            List<CategoryEntity> categoryEntities = listResponseVo.getData();

            //缓存数据到redis
            if (CollectionUtils.isEmpty(categoryEntities)) {
                //为了防止缓存穿透，数据即使为空也缓存，但是缓存时间一般不超过5min
                this.redisTemplate.opsForValue().set(KEY_PREFIX + pid, JSON.toJSONString(categoryEntities), 5, TimeUnit.MINUTES);
            } else {
                // 为了防止缓存雪崩，给过期时间添加随机值
                this.redisTemplate.opsForValue().set(KEY_PREFIX + pid, JSON.toJSONString(categoryEntities), 960 + new Random().nextInt(20), TimeUnit.HOURS);
            }

            return categoryEntities;
        } finally {
            fairLock.unlock();
        }
    }

    public void testLock() {
        RLock lock = this.redissonClient.getLock("lock"); // 只要锁的名称相同就是同一把锁
        lock.lock(); // 加锁

        //查询redis中的num值
        String value = this.redisTemplate.opsForValue().get("num");
        if (StringUtils.isBlank(value)) {
            return;
        }
        int num = Integer.parseInt(value);
        this.redisTemplate.opsForValue().set("num", String.valueOf(++num));

        lock.unlock(); // 解锁
    }

    /**
     * 优化之可重入锁，基于Redis Hash实现方案存储的锁的重入次数。
     * 加锁和解锁够加了续期
     */
    public void testLock3() {

        String uuid = UUID.randomUUID().toString();
        Boolean lock = this.distributeLock.tryLock("lock", uuid, 30);

        if (lock) {
            String json = this.redisTemplate.opsForValue().get("num");
            if (StringUtils.isBlank(json)) {
                this.redisTemplate.opsForValue().set("num", "1");
            }
            int num = Integer.parseInt(json);
            this.redisTemplate.opsForValue().set("num", String.valueOf(++num));

            //this.testSubLock(uuid);
            try {
                TimeUnit.SECONDS.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

            this.distributeLock.unlock("lock", uuid);
        }
    }

    public void testSubLock(String uuid) {
        this.distributeLock.tryLock("lock", uuid, 30);
        System.out.println("测试可重入锁");
        this.distributeLock.unlock("lock", uuid);
    }

    /**
     * 优化之LUA脚本保证删除的原子性
     */
    public void testLock2() {
        // 1.加锁
        String uuid = UUID.randomUUID().toString();
        Boolean flag = this.redisTemplate.opsForValue().setIfAbsent("lock", uuid, 3, TimeUnit.SECONDS);
        if (!flag) {
            // 3.如果没有抢到锁，重试
            try {
                Thread.sleep(80);
                testLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            //this.redisTemplate.expire("lock", 10, TimeUnit.SECONDS);

            String json = this.redisTemplate.opsForValue().get("num");
            if (StringUtils.isBlank(json)) {
                this.redisTemplate.opsForValue().set("num", "1");
            }
            int num = Integer.parseInt(json);
            this.redisTemplate.opsForValue().set("num", String.valueOf(++num));

            // 2.解锁：防止误删，先判断是否自己的锁，如果是自己的锁才能释放
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            this.redisTemplate.execute(new DefaultRedisScript<>(script, Boolean.class), Arrays.asList("lock"), uuid);
            //if (StringUtils.equals(uuid, this.redisTemplate.opsForValue().get("lock"))){
            //   this.redisTemplate.delete("lock");
            //}
        }
    }

    public void testLock1() {

        String json = this.redisTemplate.opsForValue().get("num");
        if (StringUtils.isBlank(json)) {
            this.redisTemplate.opsForValue().set("num", "1");
        }
        int num = Integer.parseInt(json);
        this.redisTemplate.opsForValue().set("num", String.valueOf(++num));

    }
}
